package com.gdwstudy.threadMethod;

/**
 * @author 关道伟
 * @version 1.0
 */
public class ThreadMethod02 {
    public static void main(String[] args) throws InterruptedException {
        T2 t2 = new T2();
        t2.start();

        for (int i = 0; i < 10; i++) {
            Thread.sleep(1000);
            if(i == 5){
                System.out.println("主线程此时让子线程独占cpu");
                //join线程插队 一定会成功
                t2.join();

                //礼让 不一定会成功
                //Thread.yield();
                System.out.println("子线程执行完毕后继续执行住主线程");
            }
            System.out.println(Thread.currentThread().getName()+"吃包子.."+i);
        }
    }
}

class T2 extends Thread {
    int i = 0;

    @Override
    public void run() {
        while (i < 10) {
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "吃包子.." + i++);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}